﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;

namespace GbopSdk
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class GbopRequest
    {
        /// <summary>
        /// 
        /// </summary>
        public string HostUrl
        {
            get; set;
        }

        /// <summary>
        /// 
        /// </summary>
        public string PATH
        {
            get; set;
        }

        /// <summary>
        /// 
        /// </summary>
        public string AK
        {
            get; set;
        }

        /// <summary>
        /// 
        /// </summary>
        public string SK
        {
            get; set;
        }

        /// <summary>
        /// 
        /// </summary>
        public GbopHttpMethod Method
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        public List<HeaderItem> Headers
        {
            get;
            private set;
        } = new List<HeaderItem>();

        /// <summary>
        /// 
        /// </summary>
        public List<QueryItem> Querys
        {
            get;
            private set;
        } = new List<QueryItem>();

        /// <summary>
        /// 
        /// </summary>
        public List<BodyItem> Bodys
        {
            get;
            private set;
        } = new List<BodyItem>();

        /// <summary>
        /// 
        /// </summary>
        public string BodyString
        {
            get
            {
                JsonSerializer serializer = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                serializer.Serialize(sw, Bodys);
                return sb.ToString();
            }
            internal set
            {
                try
                {
                    JsonSerializer serializer = new JsonSerializer();
                    StringReader sr = new StringReader(value);
                    Bodys = serializer.Deserialize(new JsonTextReader(sr), typeof(List<BodyItem>)) as List<BodyItem>;
                }
                catch
                {

                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string QueryString
        {
            get
            {
                JsonSerializer serializer = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                serializer.Serialize(sw, Querys);
                return sb.ToString();
            }
            internal set
            {
                try
                {
                    JsonSerializer serializer = new JsonSerializer();
                    StringReader sr = new StringReader(value);
                    Querys = serializer.Deserialize(new JsonTextReader(sr), typeof(List<QueryItem>)) as List<QueryItem>;
                }
                catch
                {

                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string HerderString
        {
            get
            {
                JsonSerializer serializer = new JsonSerializer();
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                serializer.Serialize(sw, Headers);
                return sb.ToString();
            }
            internal set
            {
                try
                {
                    JsonSerializer serializer = new JsonSerializer();
                    StringReader sr = new StringReader(value);
                    Headers = serializer.Deserialize(new JsonTextReader(sr), typeof(List<HeaderItem>)) as List<HeaderItem>;
                }
                catch
                {

                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool EnableUrlEncode
        {
            get; set;
        }

        /// <summary>
        /// 
        /// </summary>
        public string Url
        {
            get; internal set;
        }

        internal string Domain
        {
            get
            {
                return HostUrl?.ToLower().Replace("http://", "").Replace("https://", "");
            }
        }

        public Dictionary<string, string> SignedHeaderDic { get; internal set; } = new Dictionary<string, string>();
        public DateTime RequestTime { get; internal set; }
        public string ContentJson { get; set; }
    }
}
